using CnControls;
using UnityEngine;
using UnityEngine.UI;

[RequireComponent(typeof(FPSController))]
public class MobileController : MonoBehaviour
{
	public float AimSensitivity = 0.1f;

	public GameObject[] controls;

    private ItemCollector[] getitems;

	private string tmp=string.Empty;

	private GameObject itemOnGround;
	private bool isOpen=false;
	private GUIItemGroundLoader guiItemGroundLoader;
	// public Button closeButton;
	public static MobileController _instance;

    private void Start()
	{
		_instance = this;
		if (controls.Length <= 0)
		{
			controls = new GameObject[base.transform.childCount];
			for (int i = 0; i < base.transform.childCount-1; i++)
			{
				controls[i] = base.transform.GetChild(i).gameObject;
			}
		}
		itemOnGround = GameObject.Find("ItemOnGround");
        guiItemGroundLoader=transform.Find("ItemOnGround").GetComponent<GUIItemGroundLoader>();
        //closeButton = GameObject.Find("Button(close)").GetComponent<Button>();
        //closeButton.onClick.AddListener(close_Button);
        itemOnGround.SetActive(false);
    }

	private void SetVisible(bool visible)
	{
		for (int i = 0; i < controls.Length-1; i++)
		{
			controls[i].SetActive(visible);
			controls[6].SetActive(false);
			//if (isOpen==false)
			//{
   //             controls[17].SetActive(isOpen);
   //         }
			//else
			//{
			//	controls[17].SetActive(isOpen);
			//}
        }
	}

	public void SwithView()
	{
		if (UnitZ.playerManager.PlayingCharacter != null)
		{
			FPSController component = UnitZ.playerManager.PlayingCharacter.GetComponent<FPSController>();
			if ((bool)component)
			{
				component.SwithView();
			}
		}
	}

	public void SwithSideView()
	{
		if (UnitZ.playerManager.PlayingCharacter != null)
		{
			FPSController component = UnitZ.playerManager.PlayingCharacter.GetComponent<FPSController>();
			if ((bool)component)
			{
				component.SwithSideView();
			}
		}
	}

	public void OpenMap()
	{
		UnitZ.Hud.TogglePanelByName("Map");
	}

	public void OpenInventory()
	{
		UnitZ.Hud.TogglePanelByName("Inventory");
	}

	public void Zoom()
	{
		if (UnitZ.playerManager.PlayingCharacter != null)
		{
			FPSController component = UnitZ.playerManager.PlayingCharacter.GetComponent<FPSController>();
			if ((bool)component)
			{
				component.Trigger2(fire: true);
			}
		}
	}

	public void Interactive()
	{
		if (UnitZ.playerManager.PlayingCharacter != null)
		{
			FPSController component = UnitZ.playerManager.PlayingCharacter.GetComponent<FPSController>();
			if ((bool)component)
			{
				component.OutVehicle();
				component.Interactive();
			}
		}
	}

	public void Reload()
	{
		if (UnitZ.playerManager.PlayingCharacter != null)
		{
			FPSController component = UnitZ.playerManager.PlayingCharacter.GetComponent<FPSController>();
			if ((bool)component)
			{
				component.Reload();
			}
		}
	}

	private void Update()
	{
		if (UnitZ.playerManager.PlayingCharacter != null)
		{
			SetVisible(visible: true);
			FPSController component = UnitZ.playerManager.PlayingCharacter.GetComponent<FPSController>();
			if ((bool)component)
			{
				MouseLock.IsMobileControl = true;
				component.MoveCommand(new Vector3(CnInputManager.GetAxis("Horizontal"), 0f, CnInputManager.GetAxis("Vertical")), CnInputManager.GetButton("Jump"));
				component.Aim(new Vector2(CnInputManager.GetAxis("Touch X") * AimSensitivity, CnInputManager.GetAxis("Touch Y") * AimSensitivity));
				component.Trigger1(CnInputManager.GetButton("Touch Fire1"));
				component.Checking();
			}

        }
		else
		{
			SetVisible(visible: false);
		}
	}
	public void detectionManifests()
	{
        ItemCollector[] allItemAround = UnitZ.playerManager.PlayingCharacter.inventory.GetAllItemAround(1f);
        string text = string.Empty;
        for (int i = 0; i < allItemAround.Length; i++)
        {
            text = text + allItemAround[i].ItemIndex + ",";
        }
		//if (text != tmp)
		//{
		//	getitems = allItemAround;
		//	tmp = text;
		//	isOpen = true;
		//	isclose_Button(isOpen);
		//	guiItemGroundLoader.Update_GUIInventory(getitems);
		//}
		if (allItemAround.Length == 0)
		{
			isOpen = false;
			isclose_Button(isOpen);

		}
        if (text != tmp)
        {
            getitems = allItemAround;
            tmp = text;
            isOpen = true;
            isclose_Button(isOpen);
            guiItemGroundLoader.Update_GUIInventory(getitems);
        }
    }
	private void isclose_Button(bool isOpen)
	{
		if (isOpen == false) {
            itemOnGround.SetActive(isOpen);
        }
		else
		{
            itemOnGround.SetActive(isOpen);
        }
	}
}
